package control;

import java.io.IOException;
import java.util.List;
import java.util.Scanner;

import matching.AimlParser;
import matching.KeyTags;

import org.jdom2.Element;

public class Chatterbot {
	final static String userFile = "database//user.xml";

	private static String getName(String sen) {
		// set Name of human
		String name = null;
		
		if (null != sen) {
			String[] words = sen.split("\\s{1,}");
			if (sen.contains("is") || sen.contains("am")) {
				for (int i = 0; i < words.length; ++i) {
					if (words[i].equals("is") || words[i].equals("am")) {
						if (i < words.length - 1) {
							name = words[i + 1];
						} else {
							name = sen;
						}
						break;
					}
				}
			} else {
				name = sen;
			}
		}
		System.out.println(name.trim()+"11");
		return name.trim();
	}
	private static boolean isUser(String name) {
		// search the name in list
		List<Element> cList = AimlParser.getInstance().XMLRead(userFile);
		for (Element e : cList) {
			if (e.getChildText(KeyTags.PATTERN).equals(name)) {
				return true;
			}
		}
		return false;
	}

	public static void main(String[] args) throws IOException {
		int count = 0;
		boolean isLearning = false;
		String sen = "";
		String name = "Human";

		// load from database
		// not same name;
		Scanner scanIn = new Scanner(System.in);
		ProcessedInput input = new ProcessedInput();

		do {
			if (0 == count) {
				System.out.println("Welcome to Chatbot 2013");
				System.out.println("Chatbot: "
						+ "Nice to meet you! Can i know your name ?");
			}
			sen = scanIn.nextLine();
			input.setInput(sen);
			if (0 == count) {
				name = getName(sen);
				System.out.println("Chatbot: " + "nice to meet you, " + name);

				// will add to list and hash map
				boolean isAnUser = isUser(name.toUpperCase());
				if (isAnUser) {
					System.out.println("Have u ever chatted with me before?");
					sen = scanIn.nextLine();
					if (null != sen) {
						sen = sen.toLowerCase();
						if (sen.contains("no")) {
							System.out.println("Oh, Let's start");
						} else {
							// load the database
							AimlParser.getInstance().loadHumanInfo(name);
							// modify here
							System.out.println("I remmember you're very "
									+ AimlParser.getInstance()
											.getHumanProperty("personality")
									+ ", aren't you? :)");
						}
					}
				} else {
					AimlParser.getInstance().addToDatabase(userFile,
							name.toUpperCase(), "", "");
					System.out.println("not a user");
				}

			} else {
				if (isLearning) {
					input.setLearning(true);
					isLearning = false;
				}
				if (sen.toLowerCase().equals("exit")) {
					System.out.println("Thank you");
					break;
				}
				
				
				ProcessedOutput answer = AimlParser.getInstance().getAnswers(
						input);

				if (null != answer) {
					System.out.println(answer.getResponse());
					if (!answer.getExist()) {
						isLearning = true;
					}
				}
			}
			++count;
		} while (!sen.toLowerCase().equals("exit"));

		// Store user info
		AimlParser.getInstance().storeHumanInfo(name);
		scanIn.close();
	}
}
